<?php 
class Tourismreviews extends CS_Controller
{
    public function _init()
    {
        $this->load->library('pagination');
        $this->load->model('tourism_order_reviews_model', 'tourism_order_reviews');
        $this->load->model('tourism_reviews_reply_model', 'tourism_reviews_reply');
    }
    
    public function grid($pg = 1)
    {
        $page_num = 20;
        $num = ($pg-1)*$page_num;
        $config['first_url'] = base_url('tourismreviews/grid').$this->pageGetParam($this->input->get());
        $config['suffix'] = $this->pageGetParam($this->input->get());
        $config['base_url'] = base_url('tourismreviews/grid');
        $config['total_rows'] = $this->tourism_order_reviews->total($this->input->get());
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_list'] = $this->pagination->create_links();
        $data['page_list'] = $this->tourism_order_reviews->page_list($page_num, $num, $this->input->get());
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['status'] = array(1=>'待审核', 2=>'通过', 3=>'未通过');
        $data['url'] = $this->pageGetParam($this->input->get());
        $this->load->view('tourismreviews/grid', $data);
    }
    
    public function edit($id)
    {
        $result = $this->tourism_order_reviews->findById($id);
        if ($result->num_rows() <= 0) {
            $this->redirect('tourismreviews/grid');
        }
        $data['tourismreviews'] = $result->row();
        $data['tourismReviewsReply'] = $this->tourism_reviews_reply->findByReviewId($id);
        $this->load->view('tourismreviews/edit', $data);
    }
    
    public function editPost()
    {
        $tourismreviews_id = $this->input->post('id');
        $error = $this->validate();
        if (!empty($error)) {
            $this->error('tourismreviews/edit', $tourismreviews_id, $error);
        }

        $this->db->trans_start();
        $resultId = $this->tourism_order_reviews->updateTourismReviews($this->input->post());
        $this->db->trans_complete();
    
        if ($resultId) {
            $this->success('tourismreviews/grid', '', '保存成功！');
        } else {
            $this->error('tourismreviews/edit', $tourismreviews_id, '保存失败！');
        }
    }
    
    public function updateStatus($pg)
    {
    	$result = $this->tourism_order_reviews->updateStatus($this->input->get());
        if($result) {
            $this->success('tourismreviews/grid/'.$pg, '', '审核成功！');
        }else{
            $this->error('tourismreviews/grid/'.$pg, '', '审核失败！');
        }
    }
    
    public function validate()
    {
        $error = array();
        if ($this->validateParam($this->input->post('status'))) {
            $error[] = '评价状态不能为空。';
        }
        if ($this->validateParam($this->input->post('review_score'))) {
            $error[] = '评价星级不能为空。';
        }
        if ($this->validateParam($this->input->post('user_content'))) {
            $error[] = '评价内容不能为空。';
        }
        return $error;
    }
    
     /**
     * 批量审核通过
     */
    public function batchSetStatus() {
        
        $type = (int)$this->input->post('type');
        $pg = (int)$this->input->post('pg');
        $reviewIdArray = $this->input->post('id');
        if (count($reviewIdArray) <=0) {
            $this->jsonMessage('请选择商品');
        }
        if (empty($type)) {
            $this->jsonMessage('请选择操作类型');
        }
        $status = ($type==1) ? 2 : 3;
        $result = $this->tourism_order_reviews->setBatchStatus($reviewIdArray, $status);
        if($result) {
            $this->session->set_flashdata('success', '操作成功!');
            $this->jsonMessage('',site_url('tourismreviews/grid/'.$pg));
        }
        $this->session->set_flashdata('error', '操作失败!');
        $this->jsonMessage('操作失败');
    }
}